package com.bw.redis.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.redis.domain.SysDictionary;
import com.bw.redis.domain.SysDictionaryContent;
import com.bw.redis.domain.SysType;
import com.bw.redis.domain.SysWay;
import com.bw.redis.service.SysDictionaryContentService;
import com.bw.redis.service.SysDictionaryService;
import com.bw.redis.service.SysTypeService;
import com.bw.redis.service.SysWayService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/dictionary")
public class RedisController {
    @Autowired
    private SysTypeService sysTypeService;
    @Autowired
    private SysWayService sysWayService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SysDictionaryService dictionaryService;
    @Autowired
    private SysDictionaryContentService dictionaryContentService;


    /**
     * 类型字典表添加redis增量同步
     * @return
     */
    @RequestMapping("/addtype")
    public R addtype(@RequestBody SysType sysType){
        boolean save = sysTypeService.save(sysType);
        if(save){
            redisService.setCacheList("typeList", Arrays.asList(sysType));
        }
        return save?R.ok():R.fail();
    }
    /**
     * 方式字典表添加redis增量同步
     * @return
     */
    @RequestMapping("/addway")
    public R addway(@RequestBody SysWay sysWay){
        boolean save = sysWayService.save(sysWay);
        if(save){
            redisService.setCacheList("wayList", Arrays.asList(sysWay));
        }
        return save?R.ok():R.fail();
    }


    /**
     * redis存储类型字典表
     * @return
     */
    @RequestMapping("/type")
    public R typeList(){
        List<SysType> list = null;
        list = redisService.getCacheList("typeList");
        if(list==null){
            list = sysTypeService.list();
            redisService.setCacheList("typeList",list);
        }
        return R.ok(list);
    }


    /**
     * 字典表列表
     * @return
     */
    @RequestMapping("/list")
    public R list(Integer current, Integer size){
        PageHelper.startPage(current,size);
        List<SysDictionary> list = dictionaryService.list();
        return R.ok(new PageInfo<>(list));
    }

    /**
     * 字典表详情查询
     * @return
     */
    @RequestMapping("/getByDid")
    public AjaxResult getByDid(String did,Integer current,Integer size){
        LambdaQueryWrapper<SysDictionaryContent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictionaryContent::getDictId,did);
        PageHelper.startPage(current,size);
        List<SysDictionaryContent> list = dictionaryContentService.list(wrapper);
        return AjaxResult.success(new PageInfo<>(list));
    }

    /**
     * 字典表添加
     * @param dictionary
     * @return
     */
    @RequestMapping("/saveDictionary")
    public AjaxResult saveDictionary(@RequestBody SysDictionary dictionary){
        boolean save = dictionaryService.save(dictionary);
        return save?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 详情字典表添加
     * @param sysDictionaryContent
     * @return
     */
    @RequestMapping("/saveDictionaryC")
    public AjaxResult saveDictionaryC(@RequestBody SysDictionaryContent sysDictionaryContent){
        boolean save = dictionaryContentService.save(sysDictionaryContent);
        return save?AjaxResult.success():AjaxResult.error();
    }
}
